package cn.jellybmedia.basic.controller;

import cn.jellybmedia.basic.exception.BusinessException;
import cn.jellybmedia.basic.service.IVerifyCodeService;
import cn.jellybmedia.basic.vo.RegisterVo;
import cn.jellybmedia.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**======================================================================================
    * @Title: getImage
    * @Description: 生成随机字符串图片验证码
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/4 16:53
    * @Parameters: [imageCodeKey]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    ======================================================================================*/
    @GetMapping ("/image/{imageCodeKey}")
    public AjaxResult getImage(@PathVariable("imageCodeKey") String imageCodeKey) {
        try {
            String base64Str = verifyCodeService.getImage(imageCodeKey);
            return AjaxResult.getAjaxResult().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }



    /**======================================================================================
    * @Title: getSmsCode
    * @Description: 发送短信验证码接收前端参数
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/5 15:24
    * @Parameters: [registerVo]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    ======================================================================================*/
    @PostMapping("/smsCode")
    public AjaxResult getSmsCode(@RequestBody RegisterVo registerVo) {
        try {
            if (registerVo.getPhone() != null){
                verifyCodeService.getSmsCode(registerVo);
            }else if (registerVo.getEmail() != null){
                verifyCodeService.getEmailCode(registerVo);
            }

            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**======================================================================================
    * @Title: getemailSmsCode
    * @Description: 店铺入住邮箱验证码发送
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/8 22:47
    * @Parameters: [registerVo]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    ======================================================================================*/
    @PostMapping("/emailSmsCode")
    public AjaxResult getemailSmsCode(@RequestBody RegisterVo registerVo) {
        try {
                verifyCodeService.getEmailCode(registerVo);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**======================================================================================
    * @Title: phoneLoginSmsCode
    * @Description: 前台手机验证登录
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/9 10:20
    * @Parameters: [registerVo]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    ======================================================================================*/
    @PostMapping("/phoneLoginSmsCode")
    public AjaxResult phoneLoginSmsCode(@RequestBody RegisterVo registerVo) {
        try {
            verifyCodeService.phoneLoginSmsCode(registerVo);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }



}
